
package gui.panels;

import coffee.CaffeineLevel;
import gui.MainFrame;
import gui.listeners.GuiActionEvent;
import gui.listeners.GuiActionEventType;
import gui.model.DrinkModel;
import gui.panels.components.MmiButton;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

/**
 *
 * @author JtR
 */
public class FinishPanel extends GuiPanel {
    private JLabel currentStatus, notice;
    private MmiButton teaImage, coffeeImage, doneCoffeeButton, doneTeaButton, favouriteCoffeeButton, favouriteTeaButton, abortCoffeeButton, abortTeaButton;
    
    
    public FinishPanel(MainFrame parent) {
        super(parent, false, BackgroundType.COFFEE);
    }
    
    @Override
    protected void initComponents() {       
        JPanel statusBox = new JPanel(new GridBagLayout());
        statusBox.setBorder(new LineBorder(Color.BLACK));
        statusBox.setBackground(Color.WHITE);
        constraints.insets = new Insets(20, 20, 0, 0);
        add(statusBox, constraints);
        
        currentStatus = new JLabel();
        currentStatus.setFont(currentStatus.getFont().deriveFont(18f).deriveFont(Font.PLAIN));
        statusBox.add(currentStatus, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(10, 10, 10, 10), 0, 0));
        
        doneCoffeeButton = new MmiButton("res/kaffeepause/brühenkaffee.png", 300);
        doneCoffeeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.DONE));
            }
        });
        constraints.anchor = GridBagConstraints.SOUTH;
        constraints.insets = new Insets(0, 0, 20, 0);
        add(doneCoffeeButton, constraints);
        doneTeaButton = new MmiButton("res/teetime/brühen.png", 300);
        doneTeaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.DONE));
            }
        });
        add(doneTeaButton, constraints);
        
        
        favouriteCoffeeButton = new MmiButton("res/kaffeepause/standart.png", 300);
        favouriteCoffeeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(FinishPanel.this, "Wirklich speichern?", "Bestätigung",  JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.AS_DEFAULT));
                    JOptionPane.showMessageDialog(FinishPanel.this, "Getränk gespeichert", "Bestätigung", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        constraints.anchor = GridBagConstraints.SOUTHEAST;
        constraints.insets = new Insets(0, 0, 20, 20);
        add(favouriteCoffeeButton, constraints);
        favouriteTeaButton = new MmiButton("res/teetime/standart.png", 300);
        favouriteTeaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(FinishPanel.this, "Wirklich speichern?", "Bestätigung",  JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.AS_DEFAULT));
                    JOptionPane.showMessageDialog(FinishPanel.this, "Getränk gespeichert", "Bestätigung", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        });
        add(favouriteTeaButton, constraints);
        
        JPanel noticePanel = new JPanel();
        constraints.anchor = GridBagConstraints.NORTHEAST;
        constraints.insets = new Insets(20, 0, 0, 20);
        noticePanel.setBorder(new LineBorder(Color.BLACK));
        noticePanel.setBackground(Color.WHITE);
        add(noticePanel, constraints);
        
        notice = new JLabel();
        notice.setFont(notice.getFont().deriveFont(18f).deriveFont(Font.PLAIN));
        noticePanel.add(notice,new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 0, 0));
        
        
        teaImage = new MmiButton("res/teetime/teeglas.png", 280);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.anchor = GridBagConstraints.SOUTHWEST;
        constraints.insets = new Insets(0, 140, 60, 0);
        add(teaImage, constraints);
        
        coffeeImage = new MmiButton("res/kaffeepause/kaffeetasse.png", 400);
        constraints.insets = new Insets(0, 50, 170, 0);
        add(coffeeImage, constraints);
        
        abortCoffeeButton = new MmiButton("res/kaffeepause/abbKaffee.png", 150);
        abortTeaButton = new MmiButton("res/teetime/abbTee2.png", 150);
        constraints.insets = new Insets(0, 0, 0, 0);
        abortCoffeeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(FinishPanel.this, "Wirklich abbrechen?", "Bestätigung", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.ABORT));
                }
            }
        });
        abortTeaButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(FinishPanel.this, "Wirklich abbrechen?", "Bestätigung", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
                    fireGuiActionEvent(new GuiActionEvent(GuiActionEventType.ABORT));
                }
            }
        });
        
        add(abortCoffeeButton, constraints);
        add(abortTeaButton, constraints);
    }

    @Override
    public String getDescription() {
        return "Bitte prüfen Sie Ihre Angaben";
    }
    
    public void setStatusLabel() {
        StringBuilder str = new StringBuilder("<html><b>Sie wählten:</b><br>");
        if(DrinkModel.getInstance().getDrink() == DrinkModel.Drink.TEA) {
            switch(DrinkModel.getInstance().getTea()) {
                case GREEN_TEA:
                    str.append("&nbsp;&nbsp;• grüner Tee<br>");
                    break;
                case BLACK_TEA:
                    str.append("&nbsp;&nbsp;• schwarzer Tee<br>");
                    break;
                case FRUIT_TEA:
                    str.append("&nbsp;&nbsp;• Früchtetee<br>");
                    break;
            }
            switch(DrinkModel.getInstance().getSize()) {
                case SMALL:
                    str.append("&nbsp;&nbsp;• klein<br>");
                    break;
                case MEDIUM:
                    str.append("&nbsp;&nbsp;• mittel<br>");
                    break;
                case BIG:
                    str.append("&nbsp;&nbsp;• groß<br>");
                    break;
            }
            str.append("&nbsp;&nbsp;• ");
            str.append(DrinkModel.getInstance().getBrewTime());
            str.append(" min bei ");
            str.append(DrinkModel.getInstance().getTemperature());
            str.append("°C");
        } else {
            switch(DrinkModel.getInstance().getCoffee()) {
                case BLACK_COFFEE:
                    str.append("&nbsp;&nbsp;• Kaffee<br>");
                    break;
                case CAPPUCHINO:
                    str.append("&nbsp;&nbsp;• Cappuchino<br>");
                    break;
                case ESPRESSO:
                    str.append("&nbsp;&nbsp;• Espresso<br>");
                    break;
            }
            switch(DrinkModel.getInstance().getSize()) {
                case SMALL:
                    str.append("&nbsp;&nbsp;• klein<br>");
                    break;
                case MEDIUM:
                    str.append("&nbsp;&nbsp;• mittel<br>");
                    break;
                case BIG:
                    str.append("&nbsp;&nbsp;• groß<br>");
                    break;
            }
            if(DrinkModel.getInstance().getCoffee() != DrinkModel.Coffee.ESPRESSO) {
                switch(DrinkModel.getInstance().getBean()) {
                    case ARABICA:
                        str.append("&nbsp;&nbsp;• Arabica-Bohnen<br>");
                        break;
                    case ROBUSTA:
                        str.append("&nbsp;&nbsp;• Robusta-Bohnen<br>");
                        break;
                }
            }
            
            if(DrinkModel.getInstance().isCaramel())
                str.append("&nbsp;&nbsp;• Karamellsirup<br>");
            
            if(DrinkModel.getInstance().isHazelnut())
                str.append("&nbsp;&nbsp;• Haselnusssirup<br>");
            
            if(DrinkModel.getInstance().isMilk())
                str.append("&nbsp;&nbsp;• Milch<br>");
        }
        str.append("</html>");
        currentStatus.setText(str.toString());
    }
    
    private void setNoticeLabel() {
        DecimalFormat df = new DecimalFormat("#0.00"); 
        int drink;
        if(DrinkModel.getInstance().getDrink() == DrinkModel.Drink.COFFEE) {
            drink = MainFrame.enumToInt(DrinkModel.getInstance().getCoffee());
        } else {
            drink = MainFrame.enumToInt(DrinkModel.getInstance().getTea());
        }
        int price = MainFrame.model.getPrice(drink, MainFrame.enumToInt(DrinkModel.getInstance().getSize()));
        int caffein = MainFrame.model.getCaffein(drink, MainFrame.enumToInt(DrinkModel.getInstance().getSize()));
        double percentage = CaffeineLevel.getCaffeineLevel(MainFrame.user.getWeight(), caffein);
        StringBuilder b = new StringBuilder("<html><b>Bitte beachten Sie:</b><br><br>=> Ihrer Koffeinskala werden<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>");
        b.append(df.format(percentage));
        b.append("%</b> zugefügt.<br>=> Ihrem Guthabenkonto werden<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>");
        b.append(df.format((float)price / 100f));
        b.append("€</b> abgezogen.");
        notice.setText(b.toString());
    }

    @Override
    public void updateFields() {
        setStatusLabel();
        setNoticeLabel();
        
        switch(DrinkModel.getInstance().getDrink()) {
            case COFFEE:
                doneCoffeeButton.setVisible(true);
                doneTeaButton.setVisible(false);
                favouriteCoffeeButton.setVisible(true);
                favouriteTeaButton.setVisible(false);
                abortCoffeeButton.setVisible(true);
                abortTeaButton.setVisible(false);
                backgroundType = BackgroundType.COFFEE;
                teaImage.setVisible(false);
                coffeeImage.setVisible(true);
                break;
            case TEA:
                doneCoffeeButton.setVisible(false);
                doneTeaButton.setVisible(true);
                favouriteCoffeeButton.setVisible(false);
                favouriteTeaButton.setVisible(true);
                abortCoffeeButton.setVisible(false);
                abortTeaButton.setVisible(true);
                backgroundType = BackgroundType.TEA;
                teaImage.setVisible(true);
                coffeeImage.setVisible(false);
                break;
        }
    }

    @Override
    public boolean getPreviousButtonEnabled() {
        return true;
    }

    @Override
    public boolean getNextButtonEnabled() {
        return false;
    }

    @Override
    public String getName() {
        return "FINISH_NAME";
    }

    @Override
    public GuiPanel getNextPanel() {
        return null;
    }

    @Override
    public GuiPanel getPreviousPanel() {
        if(DrinkModel.getInstance().getDrink() == DrinkModel.Drink.COFFEE) {
            switch(DrinkModel.getInstance().getCoffee()) {
                case BLACK_COFFEE:
                case ESPRESSO:
                    return parent.selectCoffeeAdditionsPanel;
                case CAPPUCHINO:
                    return parent.selectCappuchinoAdditionsPanel;
                default:
                    throw new RuntimeException();
            }
        } else {
            return parent.selectBrewTimePanel;
        }
    }
}
